package cn.kgc.ssm.controller;

import cn.kgc.ssm.annotations.LogOperation;
import cn.kgc.ssm.dto.QueryUser;
import cn.kgc.ssm.entity.User;
import cn.kgc.ssm.enums.Operation;
import cn.kgc.ssm.form.UserLoginForm;
import cn.kgc.ssm.service.UserService;
import cn.kgc.ssm.util.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author 课工场
 * @date 2023/10/19
 * @description
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;


    //分页查询
    @GetMapping("selectUserPage")
    public ResponseResult userPage(QueryUser userPageDTO){
        return   userService.userPage(userPageDTO);
    }

    //  用户的注册
    @PostMapping("register")
    @LogOperation(description = Operation.ADD,model = "用户模块")
    public ResponseResult registerUser(User user){

       return userService.registerUser(user);

    }

    // 用户登录
    @GetMapping("login")
    public ResponseResult login(UserLoginForm loginForm, HttpSession httpSession){
        return userService.login(loginForm,httpSession);
    }

    // 个人资料修改
    @GetMapping("getUserByName")
    public ResponseResult getUserByName(String username){

        return userService.getUserByName(username);

    }

    @PostMapping("edit")
    public ResponseResult editInfo(@RequestBody User user){

        return userService.editInfo(user);

    }

    // 修改密码
    @GetMapping("changePwd")
    public  ResponseResult  changePwd(String oldPwd,String newPassword,HttpSession session){

        return  userService.changePwd(oldPwd, newPassword,session);
    }

    // 退出登录
    @GetMapping("logout")
    public  ResponseResult  logout(HttpSession session){

        // 销毁session
        session.invalidate();

        return  ResponseResult.success();
    }


    // 添加用户信息
    @PostMapping("add")
    @LogOperation(description = Operation.ADD,model = "用户模块")
    public ResponseResult  addUser(@RequestBody User user){
      return   userService.addUser(user);
    }

    // 根据用户名获取用户信息
    @GetMapping("getByUsername")
    public ResponseResult  getByUsername(String username){
        return  userService.getUserByName(username);
    }



    //删除用户信息     user/delete?ids=1&ids=2
    @GetMapping("delete")
    public ResponseResult  batchDelete(Integer[] ids){
        return  userService.batchDelete(ids);
    }

}
